﻿Public Class F15B_tra_Tin_Dung
    Dim the As New TheTinDung()
    Dim DS As New List(Of CTTraTD)
    'Dim sotien, thanhtoan As Integer

    'Sub TinhToan()
    '    Dim ngay1, ngay2 As Date
    '    Dim d = XL_TheTinDung.Lay(Me.Tag).NgayLap.Day
    '    Dim n = Integer.Parse(TextBox_nam.Text)
    '    If Now.Month = 1 Then
    '        ngay1 = New Date(n - 1, 12, d)
    '    Else
    '        ngay1 = New Date(n, Now.Month - 1, d)
    '    End If
    '    ngay2 = New Date(Now.Year, Now.Month, d)
    '    sotien = XL_CTCaThe.TienThang(Me.Tag, ngay1, ngay2)
    '    TextBox_sotien.Text = sotien.ToString()
    '    thanhtoan = XL_CTTraTD.DaThanhToan(Me.Tag, ngay1, ngay2)
    '    TextBox_dathanhtoan.Text = sotien.ToString()
    '    TextBox_conlai.Text = (sotien - thanhtoan).ToString()
    'End Sub

    Sub HienThi()
        lsvphieuchi.Items.Clear()
        DS = XL_CTTraTD.LayDS(the.ID)
        For Each ct In DS
            Dim li As New ListViewItem(ct.Ngay)
            li.Tag = ct
            li.SubItems.Add(ct.SoTien)
            li.SubItems.Add(ct.DaThanhToan)
            li.SubItems.Add(ct.ConLai)
            li.SubItems.Add(ct.LaiSuat)
            li.SubItems.Add(ct.TongNo)
            lsvphieuchi.Items.Add(li)
        Next
    End Sub


    Private Sub F15B_tra_Tin_Dung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        the = Me.Tag
        Labelsothe.Text = the.TaiKhoan
        HienThi()
        Labelno.Text = XL_CTTraTD.No(the.ID)

        ComboBox1.DataSource = XL_LoaiChi.LayDS()
        ComboBox1.DisplayMember = "Loai"
        ComboBox1.ValueMember = "ID"

        ComboBox2.DataSource = XL_TaiKhoan.LayDS(the.NguoiDung)
        ComboBox2.DisplayMember = "TenTK"
        ComboBox2.ValueMember = "ID"
    End Sub


    Private Sub lsvphieuchi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvphieuchi.SelectedIndexChanged
        Try
            Dim ct As CTTraTD = lsvphieuchi.SelectedItems(0).Tag
            DateTimePicker_ngay.Value = ct.Ngay
            TextBox_sotien.Text = ct.SoTien.ToString()
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub


    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        If MessageBox.Show("Bạn muốn thoát?", "Thông báo!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            Dim ct As New CTTraTD
            ct.Ngay = DateTimePicker_ngay.Value.Date
            ct.SoTien = Integer.Parse(TextBox_sotien.Text)
            ct.MaTD = the.ID
            ct.DaThanhToan = 0
            ct.ConLai = ct.SoTien - ct.DaThanhToan
            ct.LaiSuat = the.LaiSuat
            Dim songay = DateDiff(DateInterval.Day, ct.Ngay.Date, Now.Date)
            If songay > 15 Then
                ct.TongNo = ct.ConLai + ct.ConLai * ct.LaiSuat / 100 * (songay - 15) / 30
            Else
                ct.TongNo = ct.ConLai
            End If
            Dim kq As Integer = XL_CTTraTD.Them(ct)
            If kq <> 0 Then
                Labelno.Text = XL_CTTraTD.No(the.ID)
                HienThi()
                MessageBox.Show("Thêm thành công!")
            Else
                MessageBox.Show("Thêm không thành công!")
            End If
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End Try
    End Sub

    'Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    '    If (lsvphieuchi.SelectedItems.Count > 0) Then
    '        Try
    '            Dim ct As CTTraTD = lsvphieuchi.SelectedItems(0).Tag
    '            ct.Ngay = DateTimePicker_ngay.Value.Date
    '            ct.DaThanhToan = 0
    '            ct.MaTD = the.ID
    '            ct.SoTien = Integer.Parse(TextBox_sotien.Text)
    '            ct.ConLai = ct.SoTien - ct.DaThanhToan
    '            Dim kq As Integer = XL_CTTraTD.CapNhat(ct)
    '            If kq <> 0 Then
    '                Labelno.Text = XL_CTTraTD.No(the.ID)
    '                HienThi()
    '                MessageBox.Show("Cập nhật thành công!")
    '            Else
    '                MessageBox.Show("Cập nhật không thành công!")
    '            End If
    '        Catch ex As Exception
    '            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    '            Exit Sub
    '        End Try
    '    Else
    '        MessageBox.Show("Chưa chọn vị trí cập nhật!", "Chú ý!")
    '    End If
    'End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        If (lsvphieuchi.SelectedItems.Count > 0) Then
            If MessageBox.Show("Ban co muon xoa vi tri da chon ?", "Chu y!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Try
                    Dim t As CTTraTD = lsvphieuchi.SelectedItems(0).Tag
                    Dim kq As Integer = XL_CTTraTD.Xoa(t)
                    If kq <> 0 Then
                        Labelno.Text = XL_CTTraTD.No(the.ID)
                        HienThi()
                        MessageBox.Show("Xoa thanh cong!")
                    Else
                        MessageBox.Show("Xoa khong thanh cong")
                    End If
                Catch ex As Exception
                    Exit Sub
                End Try
            End If
        Else
            MessageBox.Show("Ban chua chon vi tri xoa!")
        End If
    End Sub

    Private Sub btn_capnhat_Click(sender As System.Object, e As System.EventArgs) Handles btn_capnhat.Click
        If (lsvphieuchi.SelectedItems.Count > 0) Then
            Try
                Dim ct As CTTraTD = lsvphieuchi.SelectedItems(0).Tag
                ct.Ngay = DateTimePicker_ngay.Value.Date
                ct.SoTien = Integer.Parse(TextBox_sotien.Text)
                ct.MaTD = the.ID
                ct.DaThanhToan = ct.DaThanhToan + Integer.Parse(tbxtt.Text)
                ct.ConLai = ct.SoTien - ct.DaThanhToan
                ct.LaiSuat = the.LaiSuat
                Dim songay = DateDiff(DateInterval.Day, ct.Ngay.Date, Now.Date)
                If songay > 15 Then
                    ct.TongNo = ct.ConLai + ct.ConLai * ct.LaiSuat / 100 * (songay - 15) / 30
                Else
                    ct.TongNo = ct.ConLai
                End If
                Dim kq As Integer = XL_CTTraTD.CapNhat(ct)
                If kq <> 0 Then
                    Labelno.Text = XL_CTTraTD.No(the.ID)
                    HienThi()

                    Dim pc As New PhieuChi
                    pc.Ngay = Now.Date
                    pc.LoaiChi = ComboBox1.SelectedValue
                    pc.NguoiChi = the.NguoiDung
                    pc.NguoiLap = the.NguoiDung
                    pc.SoTien = Integer.Parse(tbxtt.Text)
                    pc.TaiKhoan = ComboBox2.SelectedValue
                    pc.ChiTiet = "Thanh toán thẻ tín dụng " + the.TaiKhoan
                    Dim kq2 = XL_PhieuChi.Them(pc)
                    If kq2 <> 0 Then
                        MessageBox.Show("Thanh toán thành công!")
                    Else
                        MessageBox.Show("Thanh toán không thành công!")
                    End If

                Else
                    MessageBox.Show("thanh toán không thành công!")
                End If
            Catch ex As Exception
                MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Exit Sub
            End Try
        Else
            MessageBox.Show("Chưa chọn vị trí thanh toán!", "Chú ý!")
        End If
    End Sub
End Class